package com.xiaolin.util;

import lombok.ToString;

import java.util.*;

/**
 * @ClassName TestComparator
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/27
 * @Version 1.0
 */
public class TestComparator {
    public static void main(String[] args) {
        Student s1=new Student("小林",18);
        Student s2=new Student("小红",17);
        List<Student> list= new ArrayList<>();
        list.add(s1);
        list.add(s2);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge() == 0 ? 0 :(o1.getAge() - o2.getAge() > 0 ? 1 : -1 );
            }
        });
        System.out.println(list);
    }
}

@ToString
class Student {
    private String name;
    private int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
